#include "file_manip.h"


int askListToServer(FILE* stream,char*** retstring,int* retnum){

	fd_set fds;
	struct timeval d;
	int sout;
	d.tv_usec = 0;
	d.tv_sec = 3;


	sendOperation(NULL,stdout,OP_LST,NULL);
	
	while(1){
		FD_ZERO(&fds);
		FD_SET(fileno(stream) ,&fds);
		sout = select ( fileno(stream)+1  , &fds , NULL , NULL , &d );
		if ( sout <= 0){
			logMessage(stderr,"Server doesnt responded, it was given about %ld seconds\n",d.tv_sec);
			return 0;
		}
		if ( FD_ISSET ( fileno (stream) , & fds)) {
			if(read_and_parsen(stream,retstring,retnum) == P_ERROR){
				exit(EXIT_FAILURE);
			}
			break;
		}
	}
	return 1;
}



int askClientListToServer(FILE* streamread,FILE* streamwrite,char*** retstring,int* retnum){

	fd_set fds;
	struct timeval d;
	int sout;
	d.tv_usec = 0;
	d.tv_sec = 3;

	sendOperation(streamread,streamwrite,OP_ALL,NULL);
	
	while(1){
		FD_ZERO(&fds);
		FD_SET(fileno(streamread) ,&fds);
		sout = select ( fileno(streamread)+1  , &fds , NULL , NULL , &d );
		if ( sout <= 0){
			logMessage(stderr,"Server doesnt responded, it was given about %ld seconds\n",d.tv_sec);
			return 0;
		}
		if ( FD_ISSET ( fileno (streamread) , & fds)) {
			if(read_and_parsen(streamread,retstring,retnum) == P_ERROR){
				exit(EXIT_FAILURE);
			}
			break;
		}
	}
	return 1;
}




char**  choseFile(char*** fileList,int sizelist,int (*isValid)(char*** fileList,int sizeL,char* file)){
    int i;
    for(i = 0;i<sizelist;i++){
        if(isValid(fileList,sizelist,(*fileList)[i])){
            return &(*fileList)[i];
        }
    }
    return NULL;
}


int sendOperation(FILE* fin,FILE* fout,int OP,char* file){
	int tmp = -1;
	int i,j;
	switch (OP){
		case OP_ADD:
			fprintf(stderr,"[>");
			for(i = 0;i<TRANSFERT_DELAI;i++){
				for(j = 0; j<4*TRANSFERT_DELAI;j++)
					fprintf(stderr,"\b=>");
				sleep(1);
			}
			fprintf(stderr,"\b=]\n");
			fprintf(fout, "ADD %s\n",file);
			fflush(fout);
			tmp = waitAnswerFromServer(fin);
			break;
		case OP_DEL:
			tmp=fprintf(fout, "DEL %s\n",file);
			fflush(fout);
			tmp = waitAnswerFromServer(fin);
			break;
		case OP_LST:
			tmp=fprintf(fout, "LST\n");
			fflush(fout);
			break;


		case OP_ALL:
			fprintf(fout, "ALL\n");
			tmp = 1;
			fflush(fout);
			break;
		case OP_REG:
			tmp=fprintf(fout, "REG %s\n",file);
			fflush(fout);
			tmp = waitAnswerFromServer(fin);
			break;
		case OP_UNR:
			tmp=fprintf(fout, "UNR %s\n",file);
			fflush(fout);
			tmp = waitAnswerFromServer(fin);
			break;
	}
	return tmp;
}


int waitAnswerFromServer(FILE* fin){
	fd_set fds;
	struct timeval d;
	int sout;
	char** retstring = NULL;
	int retnum = 0;
	
	d.tv_usec = 0;
	d.tv_sec = 3;

	while(1){
		FD_ZERO (&fds);
		FD_SET (fileno(fin) ,&fds);
		sout = select ( fileno(fin)+1  , &fds , NULL , NULL , &d );
		if ( sout <= 0){
			return 0;
		}
		if ( FD_ISSET ( fileno ( fin) , & fds)) {
			if(read_and_parsen(fin,&retstring,&retnum) == P_ERROR){
				exit(EXIT_FAILURE);
			}
			break;
		}else {
			continue;
		}
	}
	if(retstring == NULL)
		return 0;
	if(retnum != 1 || strcmp(ANSWER_OK,(retstring)[0]) != 0){
		return 0;
	}
	return 1;
}


